
#include "TeacherPhoneDbSavingController.h"


#include "../../../DAO/Savers/TeacherPhoneSavers/DbSaver/TeacherPhoneDbSaver.h"

#include "../../../Validators/PhoneValidators/PhoneCategoryValidators/TelephoneCategoryValidator/TelephoneCategoryValidator.h"
#include "../../../Validators/PhoneValidators/PhoneNumberValidators/HomeTelephoneNumberValidator/HomeTelephoneNumberValidator.h"
#include "../../../Validators/TeacherValidators/TeacherInsuranceNumberValidator/AfmPolicy/AfmPolicy.h"

//*
//	 * Empty constructor which does nothing
TeacherPhoneDbSavingController::TeacherPhoneDbSavingController(){

	// TODO Auto-generated constructor stub

}

//*
//	 * Does nothing
TeacherPhoneDbSavingController::~TeacherPhoneDbSavingController(){

	// TODO Auto-generated destructor stub
}

//*
//	 *	Implements the business politic for saving teacher phone to database
//	 *
//	 *	\param	insuranceNumber, owner's insurance number
//	 *	\param	number, the phone number
//	 *	\param	category, phone's category (ex. home,fax,mobile,...)
//	 *
//	 *	\return true, if teacher's phone has been saved
bool TeacherPhoneDbSavingController::saveTeacherPhone(const string * insuranceNumber, const string * number, const string * category) {

	bool insertionSucceed = false;
	if((NULL!=insuranceNumber)&&(NULL!=number)&&(NULL!=category)){

		/*-- Business Policy--*/
		if(InsuranceNumberPassWithBusinessPolicy(insuranceNumber)){
			if(phonePassWithBusinessPolicy(number,category)){

				ITeacherPhoneSaver *phoneSaver = new TeacherPhoneDbSaver;
				insertionSucceed = phoneSaver->saveTeacherPhone(insuranceNumber,number,category);


			}
		}
	}
	return insertionSucceed;

}

bool	TeacherPhoneDbSavingController::phonePassWithBusinessPolicy(const string * number, const string * category){
	bool	agreeWithPolicy = false;

	if((NULL!=number)&&(NULL!=category)){

		ITelephoneCategoryValidator	*categoryValidator = new TelephoneCategoryValidator;
		if(categoryValidator->phoneCategoryIsCorrect(category)){
			IPhoneNumberValidator		*numberValidator = new HomeTelephoneNumberValidator;
			if((numberValidator->phoneNumberHasAppropriateLength(number))&&(numberValidator->phoneNumberHasAppropriateContent(number))){

				agreeWithPolicy = true;

			}
			delete numberValidator;
		}
		delete categoryValidator;
	}
	return agreeWithPolicy;
}

bool	TeacherPhoneDbSavingController::InsuranceNumberPassWithBusinessPolicy(const string *insuranceNumber){
	bool	agreeWithPolicy = false;

	if(NULL!=insuranceNumber){

		ITeacherInsuranceNumberValidator *insuranceNumberValidator = new AfmPolicy;
		agreeWithPolicy=(insuranceNumberValidator->insuranceNumberAgreeWithBusinessPolicy(insuranceNumber))?true:false;
		delete insuranceNumberValidator;
	}
	return agreeWithPolicy;
}
